/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.domain.Collect;
import com.sesame.domain.Friend;
import com.sesame.domain.Image;
import com.sesame.domain.User;
import com.sesame.domain.Album;
import com.sesame.domain.Guestbook;
import com.sesame.service.AlbumService;
import com.sesame.service.ContactService;
import com.sesame.service.MessageService;
import com.sesame.service.PageService;
import com.sesame.service.UserService;
import com.sesame.toolkit.Page;

/**
 * @author shubeta
 * 
 */
public class HomeController extends AbstractController {

    private UserService userService;
    private AlbumService albumService;
    private ContactService contactService;
    private MessageService messageService;
    private PageService pageService;

    public void setPageService(PageService pageService) {
        this.pageService = pageService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    public void setContactService(ContactService contactService) {
        this.contactService = contactService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        UserDetails userDetails = (UserDetails) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        User user = userService.showUser(userDetails.getUsername());
        request.getSession().setAttribute("userId", user.getId());
        request.getSession().setAttribute("nickName", user.getNickName());
        request.getSession().setAttribute("headPic", user.getHeadPic());

        // 分页相关

        Integer userId = (Integer) request.getSession().getAttribute("userId");

        Map model = new HashMap();
        List<Album> albumList = null;
        List<Image> imageList = null;
        Integer guestbookId = null;
        if (request.getParameter("guestbookId") != null) {
            guestbookId = Integer.parseInt(request.getParameter("guestbookId"));
        }

        String nickName = (String) request.getSession()
                .getAttribute("nickName");
        String headPic = (String) request.getSession().getAttribute("headPic");
        String content = request.getParameter("content");
        String reply = request.getParameter("reply");

        if (content != null && !content.equals("")) {
            contactService.newGuestbook(userId, userId, content, nickName,
                    headPic);
        }

        if (reply != null && guestbookId != null && !reply.equals("")) {
            contactService.reply(reply, guestbookId);
        }

        albumList = albumService.listTopAlbumByUserId(userId);
        imageList = albumService.listTopImage(userId);

        List<Guestbook> guestbookList = contactService.listTopGuestbook(userId);
        List<Friend> friendList = contactService.listTopFriend(userId);
        List<Collect> collectList = userService.listTopCollect(userId);

        model.put("albumList", albumList);
        model.put("imageList", imageList);
        model.put("guestbookList", guestbookList);
        model.put("friendList", friendList);
        model.put("collectList", collectList);
        model.put("writerId", userId);
        model.put("user", user);

        return new ModelAndView("home", model);

    }

}